package br.ufrgs.inf.engsoft.stp.controller.product;

import br.ufrgs.inf.engsoft.stp.controller.StpController;
import br.ufrgs.inf.engsoft.stp.controller.exchange.ExchangeProductController;
import br.ufrgs.inf.engsoft.stp.product.Product;
import br.ufrgs.inf.engsoft.stp.ui.HTMLabel;
import br.ufrgs.inf.engsoft.stp.user.User;
import br.ufrgs.inf.engsoft.stp.view.product.ProductInfoView;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;

@SuppressWarnings("serial")
public class ProductInfoController implements StpController {

	private ProductInfoView view;
	private ExchangeProductController exchange;
	private Product p;
	
	public ProductInfoController(Product p) {
		this.p = p;
		view = new ProductInfoView();
		setupViewLabels(p);
		setupButtonCreateExchange();
	}
	
	private void setupViewLabels(Product p) {
		view.getName().setValue(p.getName(),HTMLabel.BOLD);
		view.getCategory().setValue(p.getCategory().getName(),HTMLabel.BOLD);
		view.getValue().setValue(p.getValue(),HTMLabel.BOLD);
		view.getImage().setSource(new ExternalResource(p.getImageURL()));
		view.getImageOffer().setVisible(false);
		view.getCategoryOffer().setVisible(false);
		view.getNameOffer().setVisible(false);
		view.getValueOffer().setVisible(false);
		view.getConfirmButton().setVisible(false);
	}
	
	private void setupButtonCreateExchange() {
		view.getExchangeButton().addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
            	exchange = new ExchangeProductController(
            	        (User) view.getApplication().getUser(), view, p);
            	exchange.initExchange();
            }
        });
	}
	
	@Override
	public CustomComponent getView() {
		return this.view;
	}
}